package com.armed.ejb.sessions;

import com.armed.ejb.entities.DetallesDeVenta;
import com.armed.ejb.transference.helpers.DataReverseTransferHelper;
import com.armed.ejb.transference.helpers.DataTransferHelper;
import com.armed.ejb.transference.ventas.DetalleVenta;
import com.armed.searchs.AbstractFilter;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

/**
 * Class DetallesDeVentaFacade
 *
 *
 * @author init() software
 * @version 24/02/2008 
 */
@Stateless
public class DetallesDeVentaFacade implements DetallesDeVentaFacadeRemote 
{
    @PersistenceContext
    private EntityManager em;
    /** Constructor of DetallesDeVentaFacade */
    public DetallesDeVentaFacade()
    {
    }

    public void create(DetalleVenta detallesDeVenta)
    {
        em.persist(DataReverseTransferHelper.revertSaleDetail(detallesDeVenta));
    }

    public void edit(DetalleVenta detallesDeVenta)
    {
        em.merge(DataReverseTransferHelper.revertSaleDetail(detallesDeVenta));
    }

    public void remove(DetalleVenta detallesDeVenta)
    {
        em.remove(em.merge(DataReverseTransferHelper.revertSaleDetail(detallesDeVenta)));
    }

    public DetalleVenta find(Object id)
    {
        DetalleVenta d = null;
        d = DataTransferHelper.copySale(em.find(com.armed.ejb.entities.DetallesDeVenta.class, id));
        return d;
    }

    public List<DetalleVenta> findAll(List<AbstractFilter> filters)
    {
        StringBuffer conditions;
        if (filters != null && filters.size() > 0)
            conditions = new StringBuffer(" WHERE");
        else
            conditions = new StringBuffer("");
        int var = 1;
        for (int i = 0; filters != null && i < filters.size(); i++)
        {
            conditions.append(" " + filters.get(i).getClause("o") + " :v" + var);
            try
            {
            if (filters.get(i + 1) != null)
                conditions.append(" AND");
            }
            catch(IndexOutOfBoundsException e)
            {
                
            }
            var++;
        }
        Query query = em.createQuery("select object(o) from DetallesDeVenta as o" + conditions.toString());
        var = 1;
        for (int i = 0; filters != null && i < filters.size(); i++)
        {
            Object value = filters.get(i).getValue();
            if (value instanceof Date)
                query.setParameter("v" + var, (Date)value,TemporalType.DATE);
            else
                query.setParameter("v" + var, value);
            var++;
        }        
        return DataTransferHelper.copySalesToList(query.getResultList());
    }

    public List<DetalleVenta> findPreferredSales(List<AbstractFilter> filters,Date initialDate, boolean showCancelled)
    {
        StringBuffer conditions;
        if (filters != null && filters.size() > 0)
            conditions = new StringBuffer(" AND");
        else
            conditions = new StringBuffer("");
        int var = 1;
        for (int i = 0; filters != null && i < filters.size(); i++)
        {
            conditions.append(" " + filters.get(i).getClause("o") + " :v" + var);
            try
            {
            if (filters.get(i + 1) != null)
                conditions.append(" AND");
            }
            catch(IndexOutOfBoundsException e)
            {
                
            }
            var++;
        }
        Query query;
        if (showCancelled)
        {
            String stm = "select object(o) from DetallesDeVenta as o WHERE o.fechaCreacion > :fecha" + conditions.toString();
            query = em.createQuery(stm);
        }
        else
        {
            String stm = "select object(o) from DetallesDeVenta as o WHERE o.fechaCreacion > :fecha and o.cancelada = :cancel" + conditions.toString();
            query = em.createQuery(stm);
            query.setParameter("cancel",DetallesDeVenta.NOT_CANCELLED_STATE);
        }
        query.setParameter("fecha",initialDate,TemporalType.DATE);
        var = 1;
        for (int i = 0; filters != null && i < filters.size(); i++)
        {
            Object value = filters.get(i).getValue();
            if (value instanceof Date)
                query.setParameter("v" + var, (Date)value,TemporalType.DATE);
            else
                query.setParameter("v" + var, value);
            var++;
        }        
        return DataTransferHelper.copySalesToList(query.getResultList());
    }
}
